package com.oa.admin.controller;

import com.joneying.common.web.response.Resp;
import com.oa.admin.dto.OutApplyAddDTO;
import com.oa.admin.dto.OutApplyListDTO;
import com.oa.admin.entity.OutApply;
import com.oa.admin.entity.OutApplyPersonnel;
import com.oa.admin.service.IOutApplyService;
import com.oa.admin.vo.OutApplyListVO;
import com.oa.core.PageResult;
import com.oa.core.exception.BusinessException;
import com.oa.core.exception.DataIsEmptyException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.joneying.common.annotation.CommonController;
import org.springframework.context.annotation.Description;

import java.util.List;

import org.springframework.web.bind.annotation.RestController;
import com.oa.core.BaseController;

/**
 * @author syb123
 * @since 2019-01-22
 */
@RestController
@RequestMapping("/outApply")
@Api(description = "外出出差申请表模块")
public class OutApplyController extends BaseController {

    private final IOutApplyService outApplyService;

    @Autowired
    public OutApplyController(IOutApplyService outApplyService) {
        this.outApplyService = outApplyService;
    }

    /**
     * 查询列表
     *
     * @return Resp
     */
    @GetMapping("")
    @CommonController
    @Description("查询所有")
    @ApiOperation(value = "查询")
    public Resp findList(OutApplyListDTO outApplyListDTO) {
        outApplyListDTO.setUserId(getLoginUserId());
        return new Resp().success(outApplyService.findOutApplyList(outApplyListDTO));
    }

    @GetMapping("/export")
    @CommonController
    @Description("导出")
    @ApiOperation(value = "导出")
    public void export(OutApplyListDTO outApplyListDTO) {
        outApplyListDTO.setUserId(getLoginUserId());
        outApplyService.export(outApplyListDTO, response);
    }


    /**
     * 新增
     *
     * @param outApplyAddDTO OutApplyAddDTO
     * @return Resp
     */
    @PostMapping("")
    @CommonController
    @Description("新增")
    @ApiOperation(value = "新增")
    public Resp add(@ApiParam(value = "外出出差申请表对象") @RequestBody(required = false) OutApplyAddDTO outApplyAddDTO) throws BusinessException, DataIsEmptyException {
        outApplyAddDTO.getOutApply().setProposer(getLoginUserId());

        if(outApplyAddDTO.getJoinOut() > 1){
            boolean repeat = false;
            for (OutApplyPersonnel outApplyPersonnel : outApplyAddDTO.getOutApplyPersonnels()) {
                if(outApplyPersonnel.getUserId() == getLoginUserId()){
                    repeat = true;
                    break;
                }
            }
            if(!repeat){
                OutApplyPersonnel outApplyPersonnel = new OutApplyPersonnel();
                outApplyPersonnel.setUserId(getLoginUserId());
                outApplyAddDTO.getOutApplyPersonnels().add(outApplyPersonnel);
            }
        }

        return outApplyService.addOutApply(outApplyAddDTO);
    }

    /**
     * 根据Id查询
     *
     * @param id id
     * @return Resp
     */
    @GetMapping("/detail/{id}")
    @CommonController
    @Description("获取详细信息")
    @ApiOperation(value = "根据id查询")
    public Resp findOutApplyDetailById(@ApiParam(value = "id", required = true) @PathVariable String id) {
        return new Resp().success(outApplyService.findOutApplyDetailById(id));
    }
}
